package com.nulldev.util.io.debug;

import java.io.IOException;
import java.io.OutputStream;

import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;

public class LoggedOutputStream extends OutputStream {

	private static final Logger log = LoggerFactory.getLogger();

	private final OutputStream wrapped;

	public LoggedOutputStream(final OutputStream os) {
		this.wrapped = os;
	}

	@Override
	public void write(final int b) throws IOException {
		log.info("[LOS] write(): " + b);
		this.wrapped.write(b);
	}

	@Override
	public void write(final byte[] b) throws IOException {
		log.info("[LOS] write(): " + b.length);
		this.wrapped.write(b);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		log.info("[LOS] write(): " + len + " (off: " + off + ")");
		this.wrapped.write(b, off, len);
	}

	@Override
	public void close() throws IOException {
		log.info("[LOS] close()");
		new Exception("close() stack").printStackTrace();
		this.wrapped.close();
	}

	@Override
	public void flush() throws IOException {
		log.info("[LOS] flush()");
		this.wrapped.flush();
	}
}
